package com.icexxx.ice_browser;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;

import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.ListModel;

public class ListManager {
    public static final String[] strs = { "AUTO", "GET", "POST", "PUT", "DELETE" };;

    public static JList<String> createList() {
        DefaultListModel<String> defaultListModel = new DefaultListModel<>();
        JList<String> jlist = new JList<String>(defaultListModel);
        jlist.setSize(192, 20);
        jlist.setBackground(Color.CYAN);
        jlist.setSelectionBackground(Color.BLACK);
        jlist.setSelectionForeground(Color.WHITE);
        Dimension preferredSize = new Dimension(192, 838);
        jlist.setPreferredSize(preferredSize);
        MouseListener mouseListener = EventManager.jListClick();
        jlist.addMouseListener(mouseListener);
        Context.put("list", jlist);
        return jlist;
    }

    public static JComboBox<String> createRequestMethodList() {
        JComboBox<String> comboBox = new JComboBox<String>();
        comboBox.setSize(30, 150);
        comboBox.setEditable(true);
        comboBox.setEnabled(true);
        Dimension preferredSize = new Dimension(70, 30);
        comboBox.setPreferredSize(preferredSize);
        for (int i = 0; i < strs.length; i++) {
            String item = strs[i];
            comboBox.addItem(item);
        }
        comboBox.setSelectedIndex(1);
        Context.put("requestMethodList", comboBox);
        return comboBox;
    }

    public static JList<String> addItem(JList<String> jlist, String item) {
        ListModel<String> listModel = jlist.getModel();
        DefaultListModel<String> defaultListModel = null;
        if (listModel instanceof DefaultListModel) {
            defaultListModel = (DefaultListModel<String>) listModel;
            defaultListModel.add(0, item);
        }
        jlist.setModel(defaultListModel);
        return jlist;
    }

    public static int calcSelectedIndex(String method) {
        for (int i = 0; i < strs.length; i++) {
            String curMethod = strs[i];
            if (curMethod.equals(method)) {
                return i;
            }
        }
        return -1;
    }
}
